package com.android.library.utils;

import java.io.Closeable;
import java.util.Collection;
import java.util.Map;

/**
 * @author xiaoxing
 * Created by Administrator on 2018/4/28.
 * 对象数据处理对象
 */

public class BeanUtils {

    public static <T> T checkNotNull(T reference) {
        if (reference == null) {
            throw new NullPointerException();
        }
        return reference;
    }


    /**
     * 判断数据是否为空
     *
     * @param obj
     * @return
     */
    @SuppressWarnings("all")
    public static boolean isEmpty(Object obj) {
        boolean flag = true;
        if (obj != null) {
            if (obj instanceof String) {
                flag = (obj.toString().trim().length() == 0);
                String tmp = obj.toString();
                flag = "".equals(tmp.trim());

            } else if (obj instanceof Collection<?>) {
                flag = ((Collection) obj).size() == 0;

            } else if (obj instanceof Map) {
                flag = ((Map) obj).size() == 0;

            } else if (obj instanceof Object[]) {
                flag = ((Object[]) obj).length == 0;

            } else {
                flag = false;
            }
        }
        return flag;
    }

    /**
     * 判断数据不为空
     * @param obj
     * @return
     */
    @SuppressWarnings("all")
    public static boolean isNotEmpty(Object obj) {
        return !isEmpty(obj);
    }

    /**
     * 通用资源的关闭操作
     *
     * @param closeable
     */
    public static void close(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
